﻿using SuperMap.Data;
using SuperMap.Realspace;
using SuperMap.UI;
using System;
using System.Collections.Generic;
using WeifenLuo.WinFormsUI.Docking;
using Ycjw.PipeLine.Map.Basic.Conditions;
using Ycjw.PipeLine.Map.Basic.SuperMap;

namespace Ycjw.PipeLine.Map.Tools.Conditions
{
    public partial class FHSearcher : ConditionPanel
    {
        private LayerWithDataListPanel LayerWithDataListPanel;
        public FHSearcher()
        {
            this.ConditionPanelName = "复合查询";
            InitializeComponent();
        }
        protected override void OnLoad(EventArgs e)
        {
            LayerWithDataListPanel = new LayerWithDataListPanel()
            {
                Dock = System.Windows.Forms.DockStyle.Fill
            };
            Scene scene = this.Scene;
            if (scene == null) return;
            LayerWithDataListPanel.Init(scene);
            //LayerWithDataListPanel.LoadToVector();   
            var types = new List<DatasetType>()
            {
                DatasetType.Line,
                DatasetType.Line3D ,
                DatasetType.Network,
                DatasetType.Network3D
            };
            LayerWithDataListPanel.LoadTo(types);
            this.uiGroupBoxbycxlx.Controls.Add(LayerWithDataListPanel);
            uiGroupBoxbycxlx.Width = this.Width / 2;
            uiGroupBoxbycxcs.Height = uiGroupBoxbycxcs.Parent.Height / 2;
            AddSqlConditions();
        }
        /*
        public override bool ConditionValid()
        {
            var dataset = LayerWithDataListPanel.SelectLayer3DDataset;
            if (dataset == null)
            {
                Sunny.UI.UIMessageTip.ShowError("选择的图层不是矢量图层");
                return false;
            }

            return base.ConditionValid();
        }
        */
        public override object Do(DockPanel dockPanel = null)
        {
            var dataset = LayerWithDataListPanel.SelectLayer3DDataset;
            if (dataset == null)
            {
                Sunny.UI.UIMessageTip.ShowError("选择的图层不是矢量图层");
                return null;
            }
            var dataSetVector = dataset.Dataset as SuperMap.Data.DatasetVector;
            bool hasGJField = false;
            var ShuXing = this.uiComboBoxshuxing.Text;
            for (int i = 0; i < dataSetVector.FieldInfos.Count; i++)
            {
                if (dataSetVector.FieldInfos[i].Name == ShuXing)
                {
                    hasGJField = true;
                    break;
                }
            }
            if (!hasGJField)
            {
                Sunny.UI.UIMessageTip.ShowError("选择的图层没有该属性信息或该参数");
                return null;
            }
            DatasetVectorEx dvEx = new DatasetVectorEx(dataset.Name)
            {
                DatasetVector = dataSetVector
            };

            Geometry geom = null;
            if (Scene.TrackingLayer.Count > 0)
            {
                geom = Scene.TrackingLayer.Get(0);
                if (geom is GeoRegion3D)
                    geom = (geom as GeoRegion3D).ConvertToRegion();
            }
            string sql = string.Empty;
            if (string.IsNullOrEmpty(this.uiTextBoxcanshu.Text))
            {
                sql = ShuXing + " IS NULL OR " + ShuXing + " = ''";
            }
            else
            {
                sql = ShuXing + " = '" + this.uiTextBoxcanshu.Text + "'";
            }
            return dvEx.Query(sql, geom);
        }
        private void AddSqlConditions()
        {
            Sunny.UI.UIRadioButton rb1 = new Sunny.UI.UIRadioButton()
            {
                Text = "矩形查询",
                Name = "RetacleQuery",
                Location = new System.Drawing.Point(20, 40)
            };
            Sunny.UI.UIRadioButton rb2 = new Sunny.UI.UIRadioButton()
            {
                Text = "圆形查询",
                Name = "CycleQuery",
                Location = new System.Drawing.Point(20, 68)
            };
            Sunny.UI.UIRadioButton rb3 = new Sunny.UI.UIRadioButton()
            {
                Text = "多边形查询",
                Name = "PolygonQuery",
                Location = new System.Drawing.Point(20, 96)
            };
            uiGroupBoxcjcx.Controls.Add(rb1);
            uiGroupBoxcjcx.Controls.Add(rb2);
            uiGroupBoxcjcx.Controls.Add(rb3);
            rb1.ValueChanged += ValueChanged;
            rb2.ValueChanged += ValueChanged;
            rb3.ValueChanged += ValueChanged;
            SceneControl sceneControl = this.SceneControl;
            if (sceneControl == null) return;

            sceneControl.Tracked += SceneControl_Tracked;
            sceneControl.Tracking += SceneControl_Tracking;
        }

        private void SceneControl_Tracking(object sender, Tracking3DEventArgs e)
        {
            if (Action3D.CreateLine != this.SceneControl.Action) return;
            var tag = "cycle_tmp";
            var index = Scene.TrackingLayer.IndexOf(tag);
            if (index > -1)
            {
                Scene.TrackingLayer.Remove(index);
            }
            GeoLine3D line3d = e.Geometry as GeoLine3D;
            if (line3d == null || line3d.Length == 0)
            {
                return;
            }
            var ps = line3d[0];
            var length = Math.Sqrt(Math.Pow((ps[0].X - ps[1].X),2) + Math.Pow((ps[0].Y - ps[1].Y), 2));
            Point2D p2d = new Point2D(ps[0].X,ps[0].Y);
            GeoRectangle gr = new GeoRectangle(p2d, length * 2, length * 2, 0);
            GeoEllipse ge = new GeoEllipse(gr);
            var gReg = ge.ConvertToRegion(40);
            var p3ds = new Point3Ds();
            for(int i =0;i < gReg[0].Count; i++)
            {
                p3ds.Add(new Point3D(gReg[0][i].X, gReg[0][i].Y,0));
            }
            var gReg3d = new GeoRegion3D(p3ds)
            {
                Style3D = new GeoStyle3D()
                {
                    LineColor = System.Drawing.Color.Yellow,
                    LineWidth = 2,
                    FillBackColor = System.Drawing.Color.FromArgb(120, System.Drawing.Color.Yellow),
                    FillForeColor = System.Drawing.Color.FromArgb(120, System.Drawing.Color.Yellow),
                    AltitudeMode = AltitudeMode.ClampToGround
                }
            };
            Scene.TrackingLayer.Add(gReg3d, tag);
        }

        private void SceneControl_Tracked(object sender, Tracked3DEventArgs e)
        {
            var geom = e.Geometry;
            if (geom == null) return;
            if (geom.Type == SuperMap.Data.GeometryType.GeoLine3D)
            {
                //Sunny.UI.UIMessageTip.ShowWarning("未实现");
            }
            else
            {
                geom.Style3D = new GeoStyle3D()
                {
                    LineColor = System.Drawing.Color.Yellow,
                    LineWidth = 2,
                    FillBackColor = System.Drawing.Color.FromArgb(120, System.Drawing.Color.Yellow),
                    FillForeColor = System.Drawing.Color.FromArgb(120, System.Drawing.Color.Yellow),
                    AltitudeMode = AltitudeMode.ClampToGround
                };
                Scene.TrackingLayer.Add(geom, string.Empty);
            }
        }
        private void ValueChanged(object sender, bool value)
        {
            Scene.TrackingLayer.Clear();
            SceneControl sceneControl = this.SceneControl;
            if (sceneControl == null) return;
            Sunny.UI.UIRadioButton btn = sender as Sunny.UI.UIRadioButton;
            if (!btn.Checked) return;
            if (btn.Name == "RetacleQuery")
            {
                sceneControl.Action = Action3D.CreateRectangle;
            }
            else if (btn.Name == "CycleQuery")
            {
                sceneControl.Action = Action3D.CreateLine;
            }
            else if (btn.Name == "PolygonQuery")
            {
                sceneControl.Action = Action3D.CreatePolygon;
            }
        }
        public override bool ConditionValid()
        {
            if (LayerWithDataListPanel == null || LayerWithDataListPanel.SelectLayer3DDataset == null)
            {
                Sunny.UI.UIMessageTip.ShowError("未获取到选中图层");
                IsConditionValid = false;
                return false;
            }
            if (!(LayerWithDataListPanel.SelectLayer3DDataset.Dataset is DatasetVector)) return false;
            return base.ConditionValid();
        }
        public override void AfterClose()
        {
            SceneControl sceneControl = this.SceneControl;
            if (sceneControl == null) return;
            sceneControl.Action = Action3D.Pan;
            sceneControl.Tracked -= SceneControl_Tracked;
            sceneControl.Tracking -= SceneControl_Tracking;
            Scene.TrackingLayer.Clear();
        }

    }
}
